﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading;
using System.Windows.Media.Imaging;
using BrainTechLLC;
using BrainTechLLC.ThreadSafeObjects;
using System.Windows.Media;

namespace BrainTechLLC.ImageOperatorsNonAForgeDependent
{
    public static partial class MediaConversion
    {
        public static List<System.Windows.Media.Color> ToMediaColors(this IEnumerable<System.Drawing.Color> colors)
        {
            List<System.Windows.Media.Color> results = new List<System.Windows.Media.Color>();

            foreach (var color in colors)
            {
                System.Windows.Media.Color c = new System.Windows.Media.Color()
                {
                    A = color.A,
                    R = color.R,
                    G = color.G,
                    B = color.B
                };

                results.Add(c);
            }

            return results;
        }

        public static BitmapPalette ToBitmapPalette(this ColorPalette palette)
        {
            BitmapPalette p = null;

            try
            {
                p = new BitmapPalette(palette.Entries.ToMediaColors());
            }
            catch { }

            return p;
        }

        public static System.Windows.Media.PixelFormat GetMediaPixelFormat(this System.Drawing.Imaging.PixelFormat pix)
        {
            switch (pix)
            {
                case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
                    return System.Windows.Media.PixelFormats.Pbgra32;

                case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
                case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
                    return System.Windows.Media.PixelFormats.Pbgra32;

                case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
                    return System.Windows.Media.PixelFormats.Bgr24;

                case System.Drawing.Imaging.PixelFormat.Format16bppArgb1555:
                    return System.Windows.Media.PixelFormats.Bgr555;

                case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
                    return System.Windows.Media.PixelFormats.Bgr555;

                case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
                    return System.Windows.Media.PixelFormats.Bgr565;

                case System.Drawing.Imaging.PixelFormat.Format16bppGrayScale:
                    return System.Windows.Media.PixelFormats.Gray16;

                case System.Drawing.Imaging.PixelFormat.Format48bppRgb:
                    return System.Windows.Media.PixelFormats.Rgb48;

                case System.Drawing.Imaging.PixelFormat.Format64bppArgb:
                    return System.Windows.Media.PixelFormats.Rgba64;

                case System.Drawing.Imaging.PixelFormat.Format64bppPArgb:
                    return System.Windows.Media.PixelFormats.Prgba64;

                case System.Drawing.Imaging.PixelFormat.Format8bppIndexed:                    
                    return System.Windows.Media.PixelFormats.Gray8;

                default:
                    Console.WriteLine("Ugh: " + pix.ToString());
                    return System.Windows.Media.PixelFormats.Bgr24;
            }
        }

     }
}
